package com.lg.mengbase.testlistview;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.lg.baselibrary.activity.BaseToolbarActivity;
import com.lg.mengbase.R;
import com.lg.mengbase.entity.ItemMsg;
import com.lg.mengbase.testlistview.adapter.MessageListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by LiGang on 2017/5/27.
 */

public class TestMoreLayoutListViewActivity extends BaseToolbarActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    @BindView(R.id.listView)
    ListView listView;
    private MessageListAdapter adapter;
    @Override
    protected int provideContentViewLayout() {
        return R.layout.activity_listview;
    }
    @Override
    protected void initTitle() {
        setToolbarTitle("BaseListViewAdapter");
    }
    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        adapter = new MessageListAdapter(this);
        adapter.setDatas(getDatas());
        listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.header,listView,false));
        listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.header,listView,false));
        listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.header,listView,false));
        listView.addFooterView(LayoutInflater.from(this).inflate(R.layout.footer,listView,false));
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
    }

    private List<ItemMsg> getDatas() {
        List<ItemMsg> datas = new ArrayList<>();
        datas.add(new ItemMsg("你好啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("在不在", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("在，有什么事？", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("木什么事啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("无聊啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("来数数啊", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("......", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("好不好伐", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("好吧，你先", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("1", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("2", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("3", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("4", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("5", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("6", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("7", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("8", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("9", ItemMsg.MessageDirection.RECEIVE));
        datas.add(new ItemMsg("10", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("继续啊", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("不会睡着了吧", ItemMsg.MessageDirection.SEND));
        datas.add(new ItemMsg("......", ItemMsg.MessageDirection.SEND));
        return datas;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(position >= listView.getHeaderViewsCount() && position < listView.getHeaderViewsCount()+adapter.getCount()){
            Toast.makeText(this, "adapter.getItem(position):" + adapter.getItem(position-listView.getHeaderViewsCount()).content, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if(position >= listView.getHeaderViewsCount() && position < listView.getHeaderViewsCount()+adapter.getCount()){
            Toast.makeText(this, "adapter.getItem(position):" + adapter.getItem(position-listView.getHeaderViewsCount()).content, Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}
